package com.sjq.project.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.sjq.project.dao.ProjectFlowDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.util.PageResults;
import com.sjq.project.entity.ProjectFlow;

@Repository
public class ProjectFlowDaoImpl extends GenericDao<ProjectFlow> implements ProjectFlowDao {

	 @Override
	 public PageResults<?> selectFlowByPage(int pageNo, int pageSize,ProjectFlow flow){
	 	StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		/*if (flow.getXxzjbh() != null&& !(flow.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(flow.getXxzjbh().trim());
		}*/
		String sql_fenye="select XXZJBH,GLRW_XXZJBH,LC_MC,LCCZR_XM,LCCZR_JH,LCCZR_SFHM,LCCZR_DWMC,LCCZR_DWBM,to_char(LC_CZSJ,'yyyy-MM-dd hh24:mi:ss') as LC_CZSJ,NEXT_LC,LC_NR from T_XM_FLOW where DJBGBS <>3 " + paramSql.toString() ;
		PageResults<Map<String, String>> results = new PageResults<Map<String, String>>();
		StringBuilder pageSql = new StringBuilder()
				.append("select * from ")
				.append(" (select tmp_1.*, rownum as rowindex from ("+sql_fenye+") tmp_1 ) tmp_2 ")
				.append(" where rowindex between ")
				.append(((pageNo-1)*pageSize + 1))
				.append(" and ")
				.append(((pageNo - 1) * pageSize + pageSize));
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(pageSql.toString(),params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql="select count(1) as num from T_XM_FLOW where DJBGBS <>3  "+paramSql.toString()+" ";
		SqlRowSet sqlRowSet = this.getJdbcTemplate().queryForRowSet(sql,params.toArray());
		int count = 0;
		while(sqlRowSet.next()){
			count = sqlRowSet.getInt("num");
		}
		results.setTotalCount(count);
        results.setPageNo(pageNo);
        results.setPageSize(pageSize);
		results.setResults(list);
		return results;	
	 }
	 @Override
	 public int addFlow(ProjectFlow flow){
	 	String sql="insert into T_XM_FLOW ( xxzjbh,glrw_xxzjbh,lc_mc,lcczr_xm,lcczr_jh,lcczr_sfhm,lcczr_dwmc,lcczr_dwbm,lc_czsj,next_lc,lc_nr) values (?,?,?,?,?,?,?,?,to_date(?,'yyyy-MM-dd hh24:mi:ss'),?,?)  ";
	 	Object [] params={flow.getXxzjbh(),flow.getGlrw_xxzjbh(),flow.getLc_mc(),flow.getLcczr_xm(),flow.getLcczr_jh(),flow.getLcczr_sfhm(),flow.getLcczr_dwmc(),flow.getLcczr_dwbm(),flow.getLc_czsj(),flow.getNext_lc(),flow.getLc_nr()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public int deleteFlow(String flowId){
		 String sql="update T_XM_FLOW set djbgbs=3 where xxzjbh=?";
		 Object [] params={ flowId};
		 return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	  @Override
	 public int updateFlow(ProjectFlow flow){
	 	String sql="update T_XM_FLOW set XXZJBH=?,GLRW_XXZJBH=?,LC_MC=?,LCCZR_XM=?,LCCZR_JH=?,LCCZR_SFHM=?,LCCZR_DWMC=?,LCCZR_DWBM=?,LC_CZSJ=?,NEXT_LC=?,LC_NR=?  where xxzjbh=?";
	 	Object [] params={flow.getXxzjbh(),flow.getGlrw_xxzjbh(),flow.getLc_mc(),flow.getLcczr_xm(),flow.getLcczr_jh(),flow.getLcczr_sfhm(),flow.getLcczr_dwmc(),flow.getLcczr_dwbm(),flow.getLc_czsj(),flow.getNext_lc(),flow.getLc_nr()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public List<Map<String, String>> findFlowByListAll(ProjectFlow flow){
		StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		if (flow.getXxzjbh() != null&& !(flow.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(flow.getXxzjbh().trim());
		}
		String sql="select XXZJBH,GLRW_XXZJBH,LC_MC,LCCZR_XM,LCCZR_JH,LCCZR_SFHM,LCCZR_DWMC,LCCZR_DWBM,to_char(LC_CZSJ,'yyyy-MM-dd hh24:mi:ss') as LC_CZSJ,NEXT_LC,LC_NR from T_XM_FLOW where DJBGBS <>3  "+ paramSql.toString() ;
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}